package com.mihu.stmall.manager.interceptor;

import com.mihu.stmall.manager.entiy.Admin;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        //执行拦截器之前来执行
        //验证登录用户
        HttpSession session =request.getSession();
        Admin admin = (Admin) session.getAttribute("admin");

        if (admin == null){
            //跳转到登录页面
            response.sendRedirect("/admin/login.html");
        }
        //放行返回true 拦截返回false
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //处理器执行完时来执行
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //所有的处理都完成时执行
    }
}
